<?php

/**
 * Description of ListPage
 *
 * @author gun
 */

class CHelper_Controller_Helper_ListPage extends Zend_Controller_Action_Helper_Abstract {
    
    public function ListPage($class = '', $page = 1, $where = array()) {
        $service = AppRegistry::loadService($class);
        $count = $service->countRows($where);
        
        $perpage = 25;
        $totalpage = ($count > 0) ? ceil($count / $perpage) : 0;        
        $offset = ($page - 1) * $perpage;        
        $view = $this->generateList($page, $totalpage);
        
        return array('perpage'=>$perpage, 'totalpage'=>$totalpage, 'offset'=>$offset, 'currentpage'=>$page, 'pview' => $view);
    }
    
    public function generateList($currentpage = 1, $totalpage = 0) {
        $html = '';
            
            $page = 1;
            while ($page <= $totalpage) {
                $html .= ($page == $currentpage)? ' <span style="padding: 7px;">'.$page.'</span> ' : 
                        ' <span style="padding: 7px;"> <a href="javascript:Web.loadPage('. $page .')">'.$page.'</a> </span> ';
                $page++;
            }
            
            $html .= '';
        
        return $html;
    }
    
    public function direct($class = '', $page = 1, $where = array()) {
        return $this->ListPage($class, $page, $where);
    }
}
